package com.wentao.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wentao.mall.exception.MallException;
import com.wentao.mall.exception.MallExceptionEnum;
import com.wentao.mall.mapper.CategoryMapper;
import com.wentao.mall.model.po.Category;
import com.wentao.mall.model.vo.CategoryVo;
import com.wentao.mall.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public void addCategory(Category category) {
        String name = category.getName();
        Category ca = categoryMapper.selectByName(name);
        if (ca!=null){
            throw new MallException(MallExceptionEnum.NAME_ALREADY_EXISTS);
        }
        int count = categoryMapper.insertSelective(category);
        if (count==0){
            throw new MallException(MallExceptionEnum.USER_INSERT_FAILED);
        }
    }

    @Override
    public void updateCategory(Category category) {
        String name = category.getName();
        Category selectCat = categoryMapper.selectByName(name);
            if (name!=null){
                if (name.equals(selectCat.getName())&& category.getId()!=selectCat.getId()){
                throw new MallException(MallExceptionEnum.NAME_ALREADY_EXISTS);
            }
        }
        int count = categoryMapper.updateByPrimaryKeySelective(category);
        if (count==0){
            throw new MallException(MallExceptionEnum.UPDATE_FAINED);
        }
    }

    @Override
    public void deleteCategory(Integer id) {
        Category category = categoryMapper.selectByPrimaryKey(id);
        if (category==null){
            throw new MallException(MallExceptionEnum.DELETE_FAILED);
        }
        int count = categoryMapper.deleteByPrimaryKey(id);
        if (count==0) {
            throw new MallException(MallExceptionEnum.DELETE_FAILED);
        }
    }

    @Override
    public PageInfo<Category> listForAdmin(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize,"type,order_num");
        List<Category> categories = categoryMapper.selectByExample(null);
        PageInfo<Category> pageInfo = new PageInfo<Category>(categories);
        return pageInfo;
    }

    @Override
    public List<CategoryVo> listForCusomer() {
        List<Category> categories=categoryMapper.selectByExample(null);
        List<CategoryVo> categoryVoList=new ArrayList<>();
        for (Category category : categories) {
            CategoryVo categoryVo=new CategoryVo();
            BeanUtils.copyProperties(category,categoryVo);
            categoryVoList.add(categoryVo);
        }
        //采用jdk8的lambda表达式的集合流，采用cpu计算，性能好
        //将voList数据转为树形菜单数据
        List<CategoryVo> tree = getTree(categoryVoList);
        return tree;
    }

    private static List<CategoryVo> getTree(List<CategoryVo> categoryVos){
        /**
         * 将集合流数据流入cpu进行遍历计算，过滤条件是父id为0（顶级菜单）就保留，否则过滤掉
         */
        List<CategoryVo> list = categoryVos.stream().filter(categoryVo -> {
            return categoryVo.getParentId()==0;}).map(categoryVo -> {
                categoryVo.setChildCategory(getChildCategoryByParentId(categoryVo.getId(),categoryVos));
                return categoryVo;
        }).collect(Collectors.toList());
        return list;
    }
    private static  List<CategoryVo> getChildCategoryByParentId(Integer parentId,List<CategoryVo> list){
        List<CategoryVo> childCategories=list.stream().filter(categoryVo -> {
            return categoryVo.getParentId()==parentId;
        }).map(categoryVo -> {categoryVo.setChildCategory(getChildCategoryByParentId(categoryVo.getId(),list));
        return categoryVo;
        }).collect(Collectors.toList());
        return childCategories;
    }
}


















